package org.manager.menu;

import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang.StringUtils.isNotBlank;

import java.io.IOException;

import javax.annotation.Resource;

import org.manager.generic.web.JsonViewUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/menu")
public class MenuController {

	@Resource(name="menuRepository")
	MenuRepository mrp;
	
	@RequestMapping(params="method=jsonData")
	public ModelAndView getMenuJsonData(){
		return JsonViewUtil.getSuccessViewWithIncludProperty(
				mrp.getMenuJsonData(), 
				new String[]{"id","name","code","parentCode","parentId","link","canBeDelete","canBeDeleteZw","checked","expanded","leaf","status"}, 
				true);
	}
	
	/**
	 * 得到多个角色对菜单的权限.
	 * @param roleIds
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params="method=menuAuthorityData")
	public ModelAndView getMenuAuthorityData(String roleIds) throws IOException{
		String roleIdArray[] = roleIds.split(",");
		return JsonViewUtil.getSuccessViewWithIncludProperty(
				mrp.getMenuAuthorityData(roleIdArray), 
				new String[]{"name","id","code","add","del","modify","import","export","checked","expanded","leaf"}, 
				true);
	}
	
	/**
	 * 添加菜单.
	 * @param menu 菜单object.
	 * @return
	 */
	@RequestMapping(params="method=add",method=RequestMethod.POST)
	public ModelAndView add(Menu menu){
		if(isBlank(menu.getParentId())) menu.setParentId(null);
		if(isBlank(menu.getParentCode())) menu.setParentCode(null);
		
		if(menu != null){
			mrp.addMenu(menu);
			return JsonViewUtil.getSuccessView();
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	/**
	 * 按照菜单id获得菜单信息.
	 * @param id Menu primary key.
	 * @return
	 */
	@RequestMapping(params="method=loadById")
	public ModelAndView loadById(String id){
		if(isNotBlank(id)){
			Menu m = mrp.get(id);
			if(m != null){
				return JsonViewUtil.getSuccessViewWithExcludProperty(m, new String[]{"childMenus","functionSet"});
			}
		}
		return JsonViewUtil.getFailureView();
	}
	
	/**
	 * 修改菜单信息.
	 * @param menu Menu object,contain primary key.
	 * @return
	 */
	@RequestMapping(params="method=modify")
	public ModelAndView modify(Menu menu){
		if(menu == null){
			return JsonViewUtil.getFailureView();
		}
		
		if(isBlank(menu.getParentId())){
			menu.setParentId(null);
		}
		
		if(isBlank(menu.getParentCode())){
			menu.setParentCode(null);
		}
		
		if(!isBlank(menu.getId())){
			mrp.update(menu);
			return JsonViewUtil.getSuccessView();
		}
		return JsonViewUtil.getFailureView();
	}
	
	/**
	 * Delete Menu.
	 * @param idarray Menus primary key array. Using ', 'division.
	 * @return
	 */
	@RequestMapping(params="method=delete")
	public ModelAndView delete(String idarray){
		String ids[] = idarray.split(",");
		int inputNum = ids.length;
		int result = mrp.deleteMenu(ids);
		
		if(result < 1){
			return JsonViewUtil.getFailureView("删除失败");
		}else if(inputNum > result){
			return JsonViewUtil.getFailureView("部分数据不允许删除!");
		}
		
		return JsonViewUtil.getSuccessView();
	}
	
	/**
	 * Load all the Menu, as List.
	 * @param request
	 * @return
	 */
	@RequestMapping(params="method=selectAll")
	public ModelAndView selectAll(){
		return JsonViewUtil.getSuccessViewWithIncludProperty(mrp.loadAll(), new String[]{"id","name","code","parentCode","parentId","link"});
	}
	
	
}
